package com.luca.framework.web.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.luca.common.CustomerUserDetails;
import com.luca.common.core.domain.entity.SysUser;
import com.luca.common.core.domain.model.LoginUser;
import com.luca.common.enums.UserStatus;
import com.luca.common.exception.BaseException;
import com.luca.common.utils.StringUtils;
import com.luca.system.domain.SysTenant;
import com.luca.system.service.ISysTenantService;
import com.luca.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * 用户验证处理
 *
 * @author luca
 */
@Slf4j
@Service
public class UserDetailsServiceImpl implements CustomerUserDetailService {

    private final ISysUserService userService;

    private final SysPermissionService permissionService;

    private final ISysTenantService tenantService;

    @Autowired
    public UserDetailsServiceImpl(ISysUserService userService, SysPermissionService permissionService, ISysTenantService tenantService) {
        this.userService = userService;
        this.permissionService = permissionService;
        this.tenantService = tenantService;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser user = userService.selectUserByUserName(username);
        checkUser(user);
        return createLoginUser(user);
    }

    public CustomerUserDetails createLoginUser(SysUser user) {
        return new LoginUser(user, permissionService.getMenuPermission(user));
    }

    @Override
    public CustomerUserDetails loadUserByCodeAndUsername(String code, String username) throws UsernameNotFoundException {
        // 运维登录
        SysUser user = null;
        if (StringUtils.isBlank(code) && StringUtils.equals("super", username)){
            user = userService.selectUserByUserName(username);
        } else {
            checkTenant(code);
            user = userService.selectUserByCodeAndUserName(code, username);
        }
        checkUser(user);
        return createLoginUser(user);
    }

    /**
     * 校验租户
     */
    private void checkTenant(String code) {
        var tenant = tenantService.getOne(new QueryWrapper<SysTenant>().eq("code", code));
        if (ObjectUtils.isEmpty(tenant)) {
            log.info("租户：{} 不存在.", code);
            throw new BaseException("对不起，您输入的单位代码：" + code + " 不存在");
        }
    }

    /**
     * 校验用户
     */
    private void checkUser(SysUser user) {
        if (ObjectUtils.isEmpty(user)) {
            log.info("登录用户：{} 不存在.", user.getUserName());
            throw new UsernameNotFoundException("登录用户：" + user.getUserName() + " 不存在");
        } else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
            log.info("登录用户：{} 已被删除.", user.getUserName());
            throw new BaseException("对不起，您的账号：" + user.getUserName() + " 已被删除");
        } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
            log.info("登录用户：{} 已被停用.", user.getUserName());
            throw new BaseException("对不起，您的账号：" + user.getUserName() + " 已停用");
        }
    }
}
